#version 150 compatibility

in   vec4 Position; // For the rasterizer
in   vec4 TexCoord;
in   vec4 Pos;
in   vec4 Color;

in vec3 fL;
in vec3 fV;
in vec4 ambient2;

// Llum
uniform    vec4 Lpos;
uniform    vec3 Ldir;
uniform    vec4 Lint_ambient;
uniform    vec4 Lint_difusa;
uniform    vec4 Lint_especular;
uniform    float Lang;

// Material
uniform   vec4 Mcomp_ambient;
uniform   vec4 Mcomp_difusa;
uniform   vec4 Mcomp_especular;
uniform   float Mcoef;

uniform float a;
uniform float b;
uniform float c;
uniform vec4 obs;
uniform vec3 vrp;
uniform vec3 dirVec;

uniform sampler1D texturaLut;
uniform sampler2D backface;

uniform sampler3D texturaVolum;
uniform sampler3D texturaMask;

uniform float stepsize;
uniform float umbralOpacitat;
uniform float anchura;
uniform float altura;
uniform float profundidad;
uniform float umbralSaturacio;
uniform int colorMode;
uniform int rcMode;
uniform float crem;
uniform vec4 colorMask;
uniform float cx;
uniform float cy;
uniform float cz;
uniform float alpha;
uniform float gamma;
uniform float k; // blending
out vec4 fColor;

uniform bool background;

const int ONE_STEP_MODE = 0;
float mascx;


vec4 getColorSample( vec3 texCoord, vec4 colorMax){
    vec4 color = vec4(0.0,0.0,0.0,0.0);
    vec4 masc = vec4(0.0,0.0,0.0,0.0);


        color = texture(texturaVolum, texCoord);//


       //     if(color.x >= umbralSaturacio){
                if(color.x > colorMax.y){
                    colorMax = color;
         //       }

        }
        return colorMax;
}



void main(void)
{
    bool dades = true;
    float fmaxi = 0.0; // valor maxim que em trobat fins a la posicio 'i'
    vec3 cfpi = vec3(0.0,0.0,0.0); // color del pixel en la posicio i
    float afpi= 0.0; // alfa del pixel en la posicio i
    float fPi =0.0;  // fPi = vec4(cfpi.x,cfpi.y,cfpi.z,afpi) es el color mes la alfa
    float alfa0 =0.0;
    float alfai = 0.0;
    vec3 c0 = vec3(0.0,0.0,0.0);
    vec3 ci = vec3(0.0,0.0,0.0);
    float deltai = 0.0;
    float betai = 0.0;
    float mascara = 0.0;
    float maximmascara = -1.0;
    bool dinsmascara = false;
    bool hihamascara = false;
    vec4 start = TexCoord; // the start position of the ray is stored in the texturecoordinate
    vec3 dir = vec3(0,0,0);
    float len = 0.0;
    vec3 caux;
    float aaux;

     dir = dirVec;
     len = length(normalize(dir.xyz));

      vec3 delta_dir = normalize(dir)* stepsize;

      vec3 vec = vec3(start.x, start.y, start.z);

      int i = 0;

      for(i; i < 3024; i++)
        {
          //color_sample = getColorSample(colorMode, vec, color_sample);

          fPi= texture(texturaVolum, vec).x;
          mascara = texture(texturaMask,vec).x;

          if ( fPi < umbralSaturacio) fPi = 0.0;



          if ( mascara < 0.0 && dinsmascara){
               //fmaxi =abs(maximmascara-fmaxi);
                deltai = abs(maximmascara-fmaxi);
                dinsmascara = false;
                maximmascara =-1.0;

          }
          else if (fPi >fmaxi || mascara > 0.0){
              deltai =abs(fPi-fmaxi);// deltai = (gamma+1)-fmaxi -> DVR
              fmaxi = fPi;
          }
          else { deltai = 0.0;}



  //       if (!(mascara>0.0 && dinsmascara)){ // efecto nube

              cfpi = vec3(fPi,fPi,fPi); // funcio de transferencia
              afpi = fPi; // *0.5;

              if ( gamma < 0.0){ betai = 1.0-deltai*(1.0+gamma);}
              else { betai = 1.0-deltai;}

              if ( mascara > 0.0){
                  if (background)cfpi = k*vec3(1.0-cx,1.0-cy,1.0-cz)+(1.0-k)*vec3(cfpi.x,cfpi.y,cfpi.z);
                  else cfpi = k*vec3(cx,cy,cz)+(1-k)*cfpi;
                  afpi = k*alpha+(1.0-k)*(afpi);
                }


              ci = betai*c0+(1-betai*alfa0)*afpi*cfpi;
              alfai = betai*alfa0+(1-betai*alfa0)*afpi;

              c0 =ci;
              alfa0 = alfai;

    //      }  // efecto nube

         if( mascara > 0.0 ){ // si hi ha mascara = estar dins la mascara
               hihamascara = true;
              if (mascara > maximmascara){
                  maximmascara = mascara;
              }
              dinsmascara =true;
          }



          vec += delta_dir;
         if((vec.x<0.00001 || vec.x>0.999999 || vec.y<0.00001 || vec.y>0.999999 ||
                   vec.z<0.000001|| vec.z>0.999999) ||alfai>0.999999) break; // terminate

         if (!dinsmascara) maximmascara =-1.0;
      }

      if ( gamma > 0.0 && alfai > 0.0){
          if (hihamascara==true) {
             if (background) caux = k*vec3(1.0-cx,1.0-cy,1.0-cz)+(1.0-k)*vec3(fmaxi,fmaxi,fmaxi);
             else caux = k*vec3(cx,cy,cz)+(1-k)*vec3(fmaxi,fmaxi,fmaxi);
             ci =(1-gamma)*ci+caux*gamma;
          }
          else
            ci =(1-gamma)*ci+vec3(fmaxi,fmaxi,fmaxi)*gamma;

          alfai =(1-gamma)*alfai+1.0*gamma;// maxim = 1.0
      }

      ci.x = ci.x*crem; if (ci.x > 1.0) ci.x = 1.0;
      ci.y = ci.y*crem; if (ci.y > 1.0) ci.y = 1.0;
      ci.z = ci.z*crem; if (ci.z > 1.0) ci.z = 1.0;

      if (background) {ci = 1.0-ci;}
      fColor =vec4(ci.x,ci.y,ci.z,alfai);

    //  if (abs(fColor.x) < 0.001 && abs(fColor.y) < 0.001 && abs(fColor.z) < 0.001 && abs(fColor.a) < 0.001)  fColor.a = 1.0;
}
